package com.jf.cloud.service.sysmanager.common.taglib;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.io.IOException;

/**
 * sk 分页标签
 * @author ouyang  
 * 当前这个分页标签是没有 20 30 50 这样的数字的 简单的分页
 * jspp@qq.com
 */
@SuppressWarnings("all")
public class PageTag_Footer extends BodyTagSupport {


	  //private static Log log=LogFactory.getLog(PageGotoTag.class);
	/**
	* 第一页按钮名称
	*/
	private static final String BUTTON_FIRST_PAGE = "pageGoto_FirstPage";

	/**
	* 上一页按钮名称
	*/
	private static final String BUTTON_PRIOR_PAGE = "pageGoto_PriorPage";

	/**
	* 下一页按钮名称
	*/
	private static final String BUTTON_NEXT_PAGE = "pageGoto_NextPage";

	/**
	* 最后页按钮名称
	*/
	private static final String BUTTON_LAST_PAGE = "pageGoto_LastPage";

	/**
	* 跳转输入框名称
	*/
	private static final String TEXT_GOTO_PAGE = "pageGoto_GotoPageInput";

	/**
	* 跳转按钮名称
	*/
	private static final String BUTTON_GOTO = "pageGoto_Goto";

	/**
	* 隐藏域记录个数名称
	*/
	private static final String HIDDEN_RECORD_COUNT = "totalRows";

	/**
	* 隐藏域页面个数名称
	*/
	private static final String HIDDEN_PAGE_COUNT = "maxPage";

	/**
	* 隐藏域当前页面名称
	*/
	private static final String HIDDEN_CURRENT_PAGE = "curPage";

	/**
	* 隐藏域跳转页面名称
	*/
	private static final String HIDDEN_GOTO_PAGE = "gotopage";

	/**
	* 隐藏域每页面显示记录数名称
	*/
	private static final String HIDDEN_ROWS_PER_PAGE = "pageSize";

	/**
	* 导航条对应的form名称
	*/
	private String formName = "";

	/**
	* 默认方法为begin
	*/
	private String methodName = "begin";

	private String IMAGE_PATH = "/platform/images/cq/navigateButton/";
 

	/**
	* 每页的记录条数
	*/
	private String rowsPerPage = String.valueOf(Paging.PAGE_SIZE);

	public String getRowsPerPage() {
	    //log.info("rowsPerPage==========="+rowsPerPage) ;
	return rowsPerPage;
	}

	public void setRowsPerPage(String rowsPerPage) {
	this.rowsPerPage = rowsPerPage;
	}

	public String getFormName() {
	return formName;
	}

	public void setFormName(String formName) {
	this.formName = formName;
	}

	public String getMethodName() {
	return methodName;
	}

	public void setMethodName(String methodName) {
	if(methodName!=null && methodName.trim().length()!=0)
	   this.methodName = methodName;
	}


	/**
	* 执行tag-lib输出
	* @throws JspException
	* @return int
	 * @throws IOException
	*/
	public int doEndTag() {
		try {
			pageContext.getOut().write(this.getHtmlString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	    return SKIP_BODY;
	}

	/**
	* 根据参数设置html输出信息
	* 包括JavaScript以及NavigateButtons
	* 将page信息设置为隐藏域，根据指定form进行提交。
	* @return String
	*/
	private String getHtmlString() {

	StringBuffer str = new StringBuffer();
	PageCalc pc = new PageCalc();
	int pageSize=10;					//分页大小
	long recordCount=0;					//记录总数，当这里为0时，则取recordCountKey
	int recordOffset=0;					//取上一页多少行
	int indexNum=5;					//索引个数
	int maxNum = 6;
	int indexSkip=1;					//索引感知点
	int pageOffset=1;					//上N页，下N页

	boolean autoScroll=false;			//索引自动滚动
	boolean createIndex=false;			//是否生成索引
	Paging pageInfo = (Paging)(((HttpServletRequest)pageContext.getRequest()).getAttribute("page"));
	if(pageInfo==null)pageInfo = new Paging();
	pageSize = pageInfo.getPageSize();
	
	long cpage=pageInfo.getCurPage();
	long lastIndex = 0;
	String spage = String.valueOf(pageInfo.getMaxPage());
	try{
		lastIndex = Long.parseLong(spage);
	} catch (Exception e){
		lastIndex=0;
	}
	
	recordCount = pageInfo.getTotalRows();
	
	pc.Init(cpage,recordCount,pageSize,recordOffset);
	pc.InitPageIndex(cpage,indexNum,indexSkip,lastIndex,autoScroll);
	/////////////构建JavaScript/////////////
	//str.append("<link href=\"css/yu_e.css\" rel=\"stylesheet\" type=\"text/css\" />" + "\n");
	//str.append("<Script Language=javaScript  src=\"js/util.js\"></Script>" + "\n");
	str.append("<SCRIPT LANGUAGE=\"JavaScript\" type=\"text/javascript\" >  " + "\n");
	
	
	
	str.append("$(function(){$('#tfooter').html($('#footer').html());});" + "\n");
	
	
	
	str.append("function gotoPage(form, actionCommand,ps) {" + "\n");
	str.append("\t" + "var currentPage = parseInt(document.getElementById(\"" + HIDDEN_CURRENT_PAGE + "\").value);" + "\n");
	str.append("\t" + "var pageCount = parseInt(document.getElementById(\"" + HIDDEN_PAGE_COUNT + "\").value);" + "\n");
	str.append("\t" + "var gotoPage = document.getElementById(\"" + HIDDEN_GOTO_PAGE + "\");" + "\n");
	str.append("\t" + "var pageSize = document.getElementById(\"" + HIDDEN_ROWS_PER_PAGE + "\");" + "\n");
	str.append("\t" + "if(actionCommand == \"" + BUTTON_FIRST_PAGE + "\") {" + "\n");
	str.append("\t" + "\t" + "gotoPage.value = 1;" + "\n");
	str.append("\t" + "}" + "\n");
	str.append("\t" + "else if(actionCommand == \"" + BUTTON_PRIOR_PAGE + "\") {" + "\n");
	str.append("\t" + "if(currentPage <= 1) {" + "\n");
	str.append("\t" + "  alert('已经是第一页了！');return ;" + "\n");
	str.append("\t" + "}" + "\n");
	str.append("\t" + "\t" + "gotoPage.value = currentPage <= 1 ? 1 : currentPage - 1;" + "\n");
	str.append("\t" + "}" + "\n");
	str.append("\t" + "else if(actionCommand == \"" + BUTTON_NEXT_PAGE + "\") {" + "\n");
	str.append("\t" + "if(currentPage >= pageCount) {" + "\n");
	str.append("\t" + "  alert('已经是最后一页了！');return ;" + "\n");
	str.append("\t" + "}" + "\n");
	str.append("\t" + "\t" + "gotoPage.value = currentPage == pageCount ? pageCount : currentPage + 1;" + "\n");
	str.append("\t" + "}" + "\n");
	str.append("\t" + "else if(actionCommand == \"" + BUTTON_LAST_PAGE + "\") {" + "\n");
	str.append("\t" + "\t" + "gotoPage.value = pageCount;" + "\n");
	str.append("\t" + "}" + "\n");
	//  20 30条数的处理---------------------add by ouyang-------------------
	str.append("\t" + "else if(ps&&(actionCommand ==10 || actionCommand ==20 || actionCommand ==30 || actionCommand ==50 || actionCommand ==100)) {" + "\n");
	str.append("\t" + "//alert(actionCommand);" + "\n");
	str.append("\t" + "\t" + "pageSize.value=actionCommand; gotoPage.value=1;" + "\n");
	str.append("\t" + "}" + "\n");
	
	//索引处理
	str.append("\t" + "else if(!isNaN(actionCommand)&&!ps) {" + "\n");
	str.append("\t" + "\t" + "gotoPage.value = actionCommand;" + "\n");
	str.append("\t" + "}" + "\n");
	
	
	str.append("\t" + "else if(actionCommand == \"" + BUTTON_GOTO + "\") {" + "\n");
	str.append("\t" + "\t" + "var ipt = document.getElementById(\"" + TEXT_GOTO_PAGE + "\");" + "\n");
	str.append("\t" + "\t" + "var index = ipt.value;" + "\n");
	str.append("\t" + "\t" + "if(index.length == 0) {" + "\n");
	str.append("\t" + "\t" + "\t" + "ipt.className=\"go_err\";" + "\n");
	str.append("\t" + "\t" + "\t" + "document.getElementById(\"" + TEXT_GOTO_PAGE + "\").focus;" + "\n");
	str.append("\t" + "\t" + "\t" + "return;" + "\n");
	str.append("\t" + "\t" + "}" + "\n");

	str.append("\t" + "if(index ==null || index < 1  || index > pageCount ) {" + "\n");
	str.append("\t" + "  alert('请输入有效的页数！');return ;" + "\n");
	str.append("\t" + "}" + "\n");
	str.append("\t" + "\t" + "gotoPage.value = index < 1 ? 1 : index > pageCount ? pageCount : index;" + "\n");
	str.append("\t" + "}" + "\n");
	//str.append("\t" + "form.action = form.action + " + "\"?method=" + this.methodName + "\";" + "\n");
	str.append("\t" + "form.target='_self';");
	str.append("\t" + "if(form.onsubmit){formBeforeSubmit();};form.submit();" + "\n");
	str.append("}" + "\n");


	str.append("function clearPage(){document.getElementById(\""+HIDDEN_GOTO_PAGE+"\").value=0;} ");
	
	str.append("function _gotoPage() {" + "\n");
	str.append("var keyCode = window.event.keyCode;" + "\n");
	str.append("if (keyCode == 13) {" + "\n");
	str.append("gotoPage(document." + this.formName + ", '" + BUTTON_GOTO + "');" + "\n");
	str.append("}" + "\n");
	str.append("}" + "\n");
	str.append("</SCRIPT>" + "\n");
	 

	/////////////获取page信息/////////////
	
	String path = ((HttpServletRequest)pageContext.getRequest()).getContextPath() + this.IMAGE_PATH;
	str.append("<div class=\"page_footer_\" id='footer'>" + "\n");
	str.append("<i class=\"page_footer_info\">共"+pageInfo.getTotalRows()+"条记录/共"+pageInfo.getMaxPage()+"页，"+pageInfo.getPageSize()+"条/页</i>" + "\n");
	
	
	if(pageInfo.getCurPage()-1<=0){
		str.append("<span class=\"curr prev\" title=\"没有上一页\"><b></b> 上一页</span>");
	}else{
		str.append("<a href=\"javascript:void(0);\" onclick=\"gotoPage(document." + this.formName + ", '" + BUTTON_PRIOR_PAGE + "');return false;\" class=\"prev\"><b></b> 上一页</a>");
	}
	
	//生成索引
	int p = pageInfo.getMaxPage();
	int p1 = p-1;
	
	if(p==1){
		str.append("<span>1</span>");
	}else{
		if (pageInfo.getGotopage()==0||pageInfo.getGotopage()==1){
			str.append("<span>1</span>");
		}else{
			if(pageInfo.getGotopage()>maxNum){
				str.append("<a href=\"javascript:void(0);\" onclick=\"gotoPage(document." + this.formName + ", '1');return false;\">1...</a>");
			}else{
				str.append("<a href=\"javascript:void(0);\" onclick=\"gotoPage(document." + this.formName + ", '1');return false;\">1</a>");
			}
		}
		
		if (pc.getTotalpage()>=1) {
			for (long i = pc.getPageIndexStart();i<=pc.getPageIndexEnd();i++){
				if(i==1||i==p){continue;}
				if (pageInfo.getGotopage()==i){
					str.append("<span>"+ i +"</span>");
				} else {
					str.append("<a href=\"javascript:void(0);\" onclick=\"gotoPage(document." + this.formName + ", '" + i + "');return false;\">"+ i +"</a>");
				}
			}
		} 
	
		/*if (pageInfo.getGotopage()==p1){
			str.append("<span>"+ p1 +"</span>");
		}else{
			str.append("<a href=\"javascript:void(0);\" onclick=\"gotoPage(document." + this.formName + ", '" + p1 + "')\">"+ p1 +"</a>");
		}*/
		
		if (pageInfo.getGotopage()==p){
			str.append("<span>"+ p +"</span>");
		}else{
			if((p-pageInfo.getGotopage())>maxNum){
				str.append("<a href=\"javascript:void(0);\" onclick=\"gotoPage(document." + this.formName + ", '" + p + "');return false;\">..."+ p +"</a>");
			}else{
				str.append("<a href=\"javascript:void(0);\" onclick=\"gotoPage(document." + this.formName + ", '" + p + "');return false;\">"+ p +"</a>");
			}
				
		}
	}


	
	if(pageInfo.getCurPage()+1<=pageInfo.getMaxPage()){
		str.append("<a href=\"javascript:void(0);\" onclick=\"gotoPage(document." + this.formName + ", '" + BUTTON_NEXT_PAGE + "');return false;\" class=\"next\">下一页 <b></b></a>");
	}else{
		str.append("<span class=\"curr next\" title=\"没有下一页\" >下一页 <b></b></span>");
	}
	
	
	
	/*str.append("<span>第"+pageInfo.getCurPage()+"页</span>");
	if(pageInfo.getCurPage()<=1){
		str.append("<span>首页</span>");
	}else{
		str.append("<a onclick=\"gotoPage(document." + this.formName + ", '" + BUTTON_FIRST_PAGE + "')\" >首页</a>");
	}
	if(pageInfo.getCurPage()-1<=0){
		str.append("<span>上一页</span>");
	}else{
		str.append("<a onclick=\"gotoPage(document." + this.formName + ", '" + BUTTON_PRIOR_PAGE + "')\" >上一页</a>");
	}
	if(pageInfo.getCurPage()+1<=pageInfo.getMaxPage()){
		str.append("<a onclick=\"gotoPage(document." + this.formName + ", '" + BUTTON_NEXT_PAGE + "')\" >下一页 </a>");
	}else{
		str.append("<span>下一页</span>");
	}
	if(pageInfo.getCurPage()==pageInfo.getMaxPage()){
		str.append("<span>末页</span>");
	}else{
		str.append("<a onclick=\"gotoPage(document." + this.formName + ", '" + BUTTON_LAST_PAGE + "')\" >末页</a>");
	}
	str.append("<span>转[<input id=\""+TEXT_GOTO_PAGE+"\" name=\""+TEXT_GOTO_PAGE+"\" type=\"text\" size=\"2\" height=\"3\">]页</span>"+ "\n");
	str.append("<a style=\"cursor:pointer;\" onclick=\"gotoPage(document." + this.formName + ", '" + BUTTON_GOTO + "')\">GO</a>");*/
	str.append("</div>" + "\n");

	/////////////构建隐藏域/////////////
	str.append("\t" + "<input type=\"hidden\" name=\"" + HIDDEN_RECORD_COUNT + "\" id=\"" + HIDDEN_RECORD_COUNT + "\" value=\"" + pageInfo.getTotalRows() + "\">" + "\n");
	str.append("\t" + "<input type=\"hidden\" name=\"" + HIDDEN_PAGE_COUNT + "\"   id=\"" + HIDDEN_PAGE_COUNT + "\"  value=\"" + pageInfo.getMaxPage() + "\">" + "\n");
	str.append("\t" + "<input type=\"hidden\" name=\"" + HIDDEN_CURRENT_PAGE + "\" id=\"" + HIDDEN_CURRENT_PAGE + "\" value=\"" + pageInfo.getCurPage() + "\">" + "\n");
	str.append("\t" + "<input type=\"hidden\" name=\"" + HIDDEN_GOTO_PAGE +"\"     id=\"" + HIDDEN_GOTO_PAGE + "\" value=\"" + pageInfo.getCurPage() + "\">" + "\n");
	str.append("\t" + "<input type=\"hidden\" name=\"" + HIDDEN_ROWS_PER_PAGE +"\" id=\"" + HIDDEN_ROWS_PER_PAGE +"\" value=\"" + pageInfo.getPageSize()+ "\">" + "\n");
	return str.toString();
	}
}

